package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.project.RCSaleCommissionTotalSettlement;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.RCSalesCommissionYearSettlement;
import com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl;
import com.shrct.businessprocess.project.impl.RCSalesCommissionYearSettlementImpl;
import com.shrct.businessprocess.project.proxy.RCSaleCommissionTotalSettlementProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALECOMMISSIONTOTALSETTLEMENT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalecommissiontotalsettlement")
public class RCSaleCommissionTotalSettlementImpl extends BaseModelObject implements RCSaleCommissionTotalSettlement {
  public RCSaleCommissionTotalSettlementImpl() {
    super();
  }
  
  @OneToMany(targetEntity = RCSalesCommissionYearSettlementImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "settlement")
  private Collection<RCSalesCommissionYearSettlement> yearParts = new java.util.ArrayList<com.shrct.businessprocess.project.RCSalesCommissionYearSettlement>();;
  
  public Collection<RCSalesCommissionYearSettlement> getYearParts() {
    return this.yearParts;
  }
  
  public RCSalesCommissionYearSettlement createAndAddtoYearParts() {
    getYearParts().size();
    com.shrct.businessprocess.project.RCSalesCommissionYearSettlement rCSalesCommissionYearSettlement = new com.shrct.businessprocess.project.impl.RCSalesCommissionYearSettlementImpl(this);
    getObjectFactory().create(rCSalesCommissionYearSettlement);
    getYearParts().add(rCSalesCommissionYearSettlement);
    return rCSalesCommissionYearSettlement;
    
  }
  
  public RCSalesCommissionYearSettlement createAndAddtoYearParts(final Procedure1<RCSalesCommissionYearSettlement> updater) {
    getYearParts().size();
    com.shrct.businessprocess.project.RCSalesCommissionYearSettlement rCSalesCommissionYearSettlement = new com.shrct.businessprocess.project.impl.RCSalesCommissionYearSettlementImpl(this);
    getObjectFactory().create(rCSalesCommissionYearSettlement);
    getYearParts().add(rCSalesCommissionYearSettlement);
    return rCSalesCommissionYearSettlement;
    
  }
  
  public RCSaleCommissionTotalSettlement removeAllYearParts() {
    for(RCSalesCommissionYearSettlement obj : this.yearParts){
    	getObjectFactory().delete(obj);
    }
    this.yearParts.clear();
    return this;	
    
  }
  
  public RCSaleCommissionTotalSettlement removeFromYearParts(final RCSalesCommissionYearSettlement rCSalesCommissionYearSettlement) {
    this.yearParts.remove(rCSalesCommissionYearSettlement);
    getObjectFactory().delete(rCSalesCommissionYearSettlement);
    return this;
    
  }
  
  @JoinColumn(name = "saler_id")
  @ManyToOne(targetEntity = RCSalesAssignmentImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesAssignment saler;
  
  public RCSalesAssignment getSaler() {
    return this.saler;
  }
  
  public RCSaleCommissionTotalSettlement setSaler(final RCSalesAssignment saler) {
    this.saler = saler;
    return this;			
    
  }
  
  private Date date;
  
  public Date getDate() {
    return this.date;
  }
  
  public RCSaleCommissionTotalSettlement setDate(final Date date) {
    this.date = date;
    return this;			
    
  }
  
  public Double getTotalCommissionAmount() {
    final Function1<RCSalesCommissionYearSettlement,Double> _function = new Function1<RCSalesCommissionYearSettlement,Double>() {
        public Double apply(final RCSalesCommissionYearSettlement it) {
          Double _commissionAmount = it.getCommissionAmount();
          return _commissionAmount;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCSalesCommissionYearSettlement, Double>map(this.yearParts, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Double getTotalOverflowCommissionAmount() {
    final Function1<RCSalesCommissionYearSettlement,Double> _function = new Function1<RCSalesCommissionYearSettlement,Double>() {
        public Double apply(final RCSalesCommissionYearSettlement it) {
          Double _overflowCommissionAmount = it.getOverflowCommissionAmount();
          return _overflowCommissionAmount;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCSalesCommissionYearSettlement, Double>map(this.yearParts, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Double getTotalTeamPrizeAmount() {
    final Function1<RCSalesCommissionYearSettlement,Double> _function = new Function1<RCSalesCommissionYearSettlement,Double>() {
        public Double apply(final RCSalesCommissionYearSettlement it) {
          Double _teamPrizeAmount = it.getTeamPrizeAmount();
          return _teamPrizeAmount;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCSalesCommissionYearSettlement, Double>map(this.yearParts, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Double getTotalAmount() {
    Double _totalCommissionAmount = this.getTotalCommissionAmount();
    Double _totalOverflowCommissionAmount = this.getTotalOverflowCommissionAmount();
    double _plus = DoubleExtensions.operator_plus(_totalCommissionAmount, _totalOverflowCommissionAmount);
    return Double.valueOf(_plus);
  }
  
  public static RCSaleCommissionTotalSettlement create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSaleCommissionTotalSettlement rCSaleCommissionTotalSettlement = new com.shrct.businessprocess.project.impl.RCSaleCommissionTotalSettlementImpl(
    );
    objectFactory.create(rCSaleCommissionTotalSettlement);
    return rCSaleCommissionTotalSettlement;			
    
  }
  
  public static RCSaleCommissionTotalSettlement createRCSaleCommissionTotalSettlementImpl(final IObjectFactory objectFactory, final Procedure1<RCSaleCommissionTotalSettlement> init) {
    com.shrct.businessprocess.project.RCSaleCommissionTotalSettlement rCSaleCommissionTotalSettlement = new com.shrct.businessprocess.project.impl.RCSaleCommissionTotalSettlementImpl(
    );
    objectFactory.create(rCSaleCommissionTotalSettlement);
    init.apply(rCSaleCommissionTotalSettlement);
    
    return rCSaleCommissionTotalSettlement;			
    
  }
  
  public RCSaleCommissionTotalSettlementProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSaleCommissionTotalSettlementProxy proxy = new com.shrct.businessprocess.project.proxy.RCSaleCommissionTotalSettlementProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
